package cn.com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @PackageName: cn.com.zpark.servlet_context
 * @ClassName: ServletContextDemo02
 * @Description:
 * @author: RZS
 * @date: 2021/11/10  11:14
 */
@WebServlet("/scd2.html")
public class ServletContextDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 通过ServletContext对象获取初始化参数
         */
        // 1、获取context对象
        ServletContext context = this.getServletContext();
        // 2、通过context对象获取全局初始化参数
        System.out.println(context.getInitParameter("name"));
        System.out.println(context.getInitParameter("age"));

        System.out.println("*************************");

        // 通过获取参数名字枚举集合获取初始化参数
        Enumeration<String> names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            // 获取初始化参数
            String value = context.getInitParameter(name);
            System.out.println(name+"："+value);
        }

    }
}






